<template>
  <div class="tinymce-editor">
    <editor
      v-model="myValue"
      :init="init"
      :disabled="disabled"
      @onClick="onClick">
    </editor>
  </div>
</template>

<script>
  import tinymce from 'tinymce/tinymce'
  import Editor from '@tinymce/tinymce-vue'
  import 'tinymce/themes/silver/theme'
  import 'tinymce/plugins/image'
  import 'tinymce/plugins/link'
  import 'tinymce/plugins/media'
  import 'tinymce/plugins/table'
  import 'tinymce/plugins/lists'
  import 'tinymce/plugins/contextmenu'
  import 'tinymce/plugins/wordcount'
  import 'tinymce/plugins/colorpicker'
  import 'tinymce/plugins/textcolor'
  import 'tinymce/plugins/fullscreen'
  import 'tinymce/plugins/print'
  import 'tinymce/plugins/preview'
  import 'tinymce/plugins/pagebreak'
  import 'tinymce/plugins/code'

  export default {
    components: {
      Editor
    },
    props: {
      value: {
        type: String,
        required: false
      },
      triggerChange: {
        type: Boolean,
        default: false,
        required: false
      },
      disabled: {
        type: Boolean,
        default: false
      },
      plugins: {
        type: [String, Array],
        default: 'lists image link media table textcolor wordcount contextmenu fullscreen preview print pagebreak '
      },
      toolbar: {
        type: [String, Array],
        default: 'undo redo |  formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists link unlink image table | removeformat  print preview pagebreak ',
        branding: false
      }
    },
    data() {
      return {
        //初始化配置
        init: {
          images_upload_base_path: '/assets',
          language_url: '/tinymce/langs/zh_CN.js',
          language: 'zh_CN',
          skin_url: '/tinymce/skins/lightgray',
          height: 300,
          plugins: this.plugins,
          toolbar: this.toolbar,
          branding: false,
          menubar: false,
          toolbar_drawer: false,
          paste_data_images: true,
          pagebreak_separator: '<br clear=all style=\'page-break-before:always\'>',
          images_upload_handler: (blobInfo, success) => {
            const img = 'data:image/jpeg;base64,' + blobInfo.base64()
            //alert(JSON.stringify(img))
            success(img)
          }
        },
        myValue: this.value
      }
    },
    mounted() {
      this.initTinymce()
    },
    methods: {
      onClick(e) {
        this.$emit('onClick', e, tinymce)
      },
      //可以添加一些自己的自定义事件，如清空内容
      clear() {
        this.myValue = ''
      },
      initTinymce() {
        window.tinymce.init({
          selector: `#${this.tinymceId}`,
          ...this.config,
          content_style: 'img {max-width:100% !important }', // 初始化赋值
          init_instance_callback: editor => {
            if (this.content) {
              editor.setContent(this.content)
            }
            this.finishInit = true
            editor.on('NodeChange Change SetContent KeyUp', () => {
              this.hasChanged = true
            })
          }, // 上传图片
          images_upload_handler: (blobInfo, success, failure) => {
            const formData = new FormData();
            formData.append('file', blobInfo.blob());
            uploadFile(formData).then(res => {
              if (res.data.code == 0) {
                let file = res.data.data;
                success(file.filePath);
                return
              }
              failure('上传失败')
            }).catch(() => {
              failure('上传出错')
            })
          }
        })
      }
    },
    watch: {
      value(newValue) {
        this.myValue = (newValue == null ? '' : newValue)
      },
      myValue(newValue) {
        if (this.triggerChange) {
          this.$emit('change', newValue)
        } else {
          this.$emit('input', newValue)
        }
      }
    }
  }

</script>